﻿using System;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using System.Collections.Generic;

namespace Winning.SolutionManager.Util
{
    internal static class DTEHelper
    {
        public const string TemplateDirectory = "Templates";
        private const string USER_PROJECT = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}";

        public static DTE GetDTEInstance()
        {
            var dte = (DTE)Package.GetGlobalService(typeof(DTE));
            return dte;
        }

        public static List<Project> GetSolutionProjects(Solution sln)
        {
            List<Project> result = new List<Project>();
            for (int i = 1; i <= sln.Projects.Count; i++) FindAllProjects(sln.Projects.Item(i), result);
            return result;
        }
        private static void FindAllProjects(Project project, List<Project> items)
        {
            if (project.Kind == USER_PROJECT) 
            {
                items.Add(project);
                return;
            }
            for (int i = 1; i <= project.ProjectItems.Count; i++)
            {
                if (project.ProjectItems.Item(i).SubProject != null) FindAllProjects(project.ProjectItems.Item(i).SubProject, items);
            }
        }
        public static Project GetActiveProject(DTE dte = null)
        {
            Project project = null;
            if(dte == null) dte = GetDTEInstance();
            if (dte != null)
            {
                var activeProjects = (Array)dte.ActiveSolutionProjects;
                if (activeProjects != null && activeProjects.Length > 0) project = activeProjects.GetValue(0) as Project;
            }
            return project;
        }

        public static ProjectItem GetTemplatesFolder()
        {
            ProjectItem templatesFolder = null;

            var project = GetActiveProject();
            if (project != null)
            {
                foreach (var item in project.ProjectItems)
                {
                    var folder = item as ProjectItem;
                    if (folder != null)
                    {
                        if (TemplateDirectory.Equals(folder.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            templatesFolder = folder;
                            break;
                        }
                    }
                }
            }
            return templatesFolder;
        }

        public static SelectedItem GetSelectedItem()
        {
            DTE dte = GetDTEInstance();

            SelectedItems items = dte.SelectedItems;

            if (items != null && items.Count == 1)
            {
                SelectedItem item = items.Item(1);
                return item;
            }
            return null;
        }

        public static bool IsFolder(this ProjectItem item)
        {
            return item.Kind == Constants.vsProjectItemKindPhysicalFolder
                || item.Kind == Constants.vsProjectItemKindVirtualFolder;
        }

        public static bool IsFile(this ProjectItem item)
        {
            return item.Kind == Constants.vsProjectItemKindPhysicalFile;
        }
    }
}